﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DesignPatternToolkit.Wizard.Engine;
using DesignPatternToolkit.Core;

namespace DesignPatternToolkit.Wizard.WizardPages
{
	public partial class PatternSelector : WizardPageBase
	{
		private Hashtable _builders = new Hashtable();
		
		public PatternSelector()
		{
			InitializeComponent();
		}

		public override void BeforeLoad()
		{
			/* Remember the current selection. */
			object selectedItem = null;
			if (patternBox.SelectedItem != null)
			{
				selectedItem = patternBox.SelectedItem;
			}
			
			patternBox.Items.Clear();
			_builders.Clear();
			
			foreach (Type type in WizardConfiguration.Instance.AvailablePatterns)
			{
				/* The name of the pattern is in the class */
				PatternBuilder builder = Activator.CreateInstance(type) as PatternBuilder;
				_builders.Add(builder.PatternName, builder);
				patternBox.Items.Add(builder.PatternName);
			}
			
			/* Reset the patternBox! */
			if (selectedItem != null)
			{
				if (_builders.ContainsKey(selectedItem.ToString()))
				{
					patternBox.SelectedItem = selectedItem;
				}
			}
		}
		
		public override void BeforeUnload()
		{
			if (patternBox.SelectedItem != null)
			{
				WizardConfiguration.Instance.PatternBuilder = _builders[patternBox.SelectedItem] as PatternBuilder;
			}
		}
	}
}
